package com.bci.pwtz.task.thread;

import org.apache.log4j.Logger;

import com.bci.pwtz.common.PwtzConstants;
import com.bci.pwtz.common.util.SpringUtil;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.service.BdzService;
import com.bci.pwtz.service.ConfigService;
import com.bci.pwtz.service.DydlfjxService;
import com.bci.pwtz.service.DygtService;
import com.bci.pwtz.service.DypdxService;
import com.bci.pwtz.service.PdbyqService;
import com.bci.pwtz.service.ZsbyqService;
import com.bci.pwtz.service.ZydlfzxService;
import com.bci.pwtz.service.ZygtService;
import com.bci.pwtz.service.ZyhwgService;
import com.bci.pwtz.service.ZykgzService;
import com.bci.pwtz.service.ZypdsService;
import com.bci.pwtz.service.ZyxsbdzService;

public class UpdateDeviceThread implements Runnable{
	private Long companyId;
	private final static Logger _logger = Logger.getLogger(UpdateDeviceThread.class);
	public UpdateDeviceThread(Long companyId)
    {
        this.companyId=companyId;
    }
	public void doWork(){
		ConfigService cfService=(ConfigService) SpringUtil.getBean("configService");
		String lastSyncTime;
		try {
			lastSyncTime=cfService.getConfigByKey("LastSyncTime");
			if(lastSyncTime!=null&&lastSyncTime!=""&&companyId!=null&&companyId!=0){
				BdzService bdzService=(BdzService)SpringUtil.getBean("bdzService");
				ZygtService zygtService=(ZygtService)SpringUtil.getBean("zygtService");
				ZyhwgService zyhwgService=(ZyhwgService)SpringUtil.getBean("zyhwgService");
				ZykgzService zykgzService=(ZykgzService)SpringUtil.getBean("zykgzService");
				ZydlfzxService zydlfzxService=(ZydlfzxService)SpringUtil.getBean("zydlfzxService");
				ZypdsService zypdsService=(ZypdsService)SpringUtil.getBean("zypdsService");
				ZyxsbdzService zyxsbdzService=(ZyxsbdzService)SpringUtil.getBean("zyxsbdzService");
				ZsbyqService zsbyqService=(ZsbyqService)SpringUtil.getBean("zsbyqService");
				PdbyqService pdbyqService=(PdbyqService)SpringUtil.getBean("pdbyqService");
				DygtService dygtService=(DygtService)SpringUtil.getBean("dygtService");
				DydlfjxService dydlfjxService=(DydlfjxService)SpringUtil.getBean("dydlfjxService");
				DypdxService dypdxService=(DypdxService)SpringUtil.getBean("dypdxService");
				bdzService.updateLastSyncDevice(companyId, lastSyncTime);
				zygtService.updateLastSyncDevice(companyId, lastSyncTime);
				zyhwgService.updateLastSyncDevice(companyId, lastSyncTime);
				zykgzService.updateLastSyncDevice(companyId, lastSyncTime);
				zydlfzxService.updateLastSyncDevice(companyId, lastSyncTime);
				zypdsService.updateLastSyncDevice(companyId, lastSyncTime);
				zyxsbdzService.updateLastSyncDevice(companyId, lastSyncTime);
				zsbyqService.updateLastSyncDevice(companyId, lastSyncTime);
				pdbyqService.updateLastSyncDevice(companyId, lastSyncTime);
				dygtService.updateLastSyncDevice(companyId, lastSyncTime);
				dydlfjxService.updateLastSyncDevice(companyId, lastSyncTime);
				dypdxService.updateLastSyncDevice(companyId, lastSyncTime);
			}
		} catch (PwtzDatabaseException e) {
			_logger.error("执行数据库操作异常："+e);
		}
	}
	@Override
	public void run() {
		try {
			Thread.sleep(PwtzConstants.THREAD_SLEEP_TIME*60*1000);
			doWork();
		} catch (Exception e) {
			_logger.error("任务执行异常"+e);
		}
	}
}
